package com.drojian.singleton.type5;

import org.junit.Test;
/**
 * 懒汉式（线程安全）双重检查，推荐使用
 */
public class SingletonTest05 {
    @Test
    public void test01(){
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance == instance2);
    }
}
class Singleton{
    private static  volatile Singleton instance;
    public Singleton() {
    }

    public static Singleton getInstance() {
        if(instance == null){
            synchronized (Singleton.class){
                if(instance == null){
                    instance = new Singleton();
                }
            }

        }
        return instance;
    }
}